/*!
    \copyright  Copyright (c) 2020 - 2023 Qualcomm Technologies International, Ltd.\n
                All Rights Reserved.\n
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file       
    \ingroup    connection_manager
    \brief      The sending of BLE events from the Connection Manager using the Event Bus
*/

#include "connection_manager_event_send_ble_link.h"
#include "connection_manager.h"

#include "ble_link_channel_event_spec.h"
#include "event_bus_publish.h"

#include <logging.h>


static void conManager_EventBusSendBleLink(ble_link_event_t event, void * data, uint16 data_size)
{
    EventBus_Publish(EVENT_BUS_CHANNEL_BLE_LINK, event, data, data_size);
}

void ConManager_EventSendBleConnected(const tp_bdaddr * tpaddr)
{
    ble_link_event_connected_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;

    DEBUG_LOG("ConManager_EventSendBleConnected");
    
    conManager_EventBusSendBleLink(ble_link_event_connected, &event, sizeof(ble_link_event_connected_t));
}

void ConManager_EventSendBleDisconnected(const tp_bdaddr * tpaddr, uint16 status)
{
    ble_link_event_disconnected_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;
    event.status = status;

    DEBUG_LOG("ConManager_EventSendBleDisconnected");
    
    conManager_EventBusSendBleLink(ble_link_event_disconnected, &event, sizeof(ble_link_event_disconnected_t));
}

void ConManager_EventSendBleConnectingFailure(const tp_bdaddr * tpaddr)
{
    ble_link_event_connecting_failure_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;

    DEBUG_LOG("ConManager_EventSendBleConnectingFailure");
    
    conManager_EventBusSendBleLink(ble_link_event_connecting_failure, &event, sizeof(ble_link_event_connecting_failure_t));
}

void ConManager_EventSendBleConnParamUpdateReq(const tp_bdaddr * tpaddr,
                                                    uint16 conn_interval_min,
                                                    uint16 conn_interval_max,
                                                    uint16 conn_latency, 
                                                    uint16 supervision_timeout)
{
    ble_link_event_conn_param_update_request_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;
    event.conn_interval_min = conn_interval_min;
    event.conn_interval_max = conn_interval_max;
    event.conn_latency = conn_latency;
    event.supervision_timeout = supervision_timeout;

    DEBUG_LOG("ConManager_EventSendBleConnParamUpdateReq");
    
    conManager_EventBusSendBleLink(ble_link_event_conn_param_update_request, &event, sizeof(ble_link_event_conn_param_update_request_t));
}

void ConManager_EventSendBleConnParamUpdateSuccess(const tp_bdaddr * tpaddr, 
                                                    uint16 conn_interval,
                                                    uint16 conn_latency, 
                                                    uint16 supervision_timeout)
{
    ble_link_event_conn_param_update_success_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;
    event.conn_interval = conn_interval;
    event.conn_latency = conn_latency;
    event.supervision_timeout = supervision_timeout;

    DEBUG_LOG("ConManager_EventSendBleConnParamUpdateSuccess");
    
    conManager_EventBusSendBleLink(ble_link_event_conn_param_update_success, &event, sizeof(ble_link_event_conn_param_update_success_t));
}

void ConManager_EventSendBleConnParamUpdateFailure(const tp_bdaddr * tpaddr, 
                                                    uint16 status)
{
    ble_link_event_conn_param_update_failure_t event = {0};
    event.connection_handle = ConManagerGetHandleFromTpAddr(tpaddr);
    event.taddr = tpaddr->taddr;
    event.status = status;

    DEBUG_LOG("ConManager_EventSendBleConnParamUpdateFailure");
    
    conManager_EventBusSendBleLink(ble_link_event_conn_param_update_failure, &event, sizeof(ble_link_event_conn_param_update_failure_t));
}                                                        

